package com.yanqu.road.dao.impl.activity.shushan;


import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.shushan.config.*;
import com.yanqu.road.entity.activity.shushan.eShuShanBuildType;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author cwq
 * @Data 2021/12/23 12:04
 */
public class ShuShanConfigDataDaoImpl extends TempDao {

    private static String getShuShanBuildConfigSql = "select * from `t_s_activity_shushan_build` where `activityId`=?";
    public List<ShuShanBuildConfig> getShuShanBuildConfigList(int activityId) {
        List<ShuShanBuildConfig> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getShuShanBuildConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ShuShanBuildConfig shuShanBuildConfig = new ShuShanBuildConfig();
                shuShanBuildConfig.setActivityId(rs.getInt("activityId"));
                shuShanBuildConfig.setId(rs.getInt("id"));
                shuShanBuildConfig.setType(rs.getInt("type"));
                shuShanBuildConfig.setLevel(rs.getInt("level"));
                shuShanBuildConfig.setUpgradCost(rs.getString("upgradCost"));
                shuShanBuildConfig.setMaxStudent(rs.getInt("maxStudent"));
                shuShanBuildConfig.setUnlock(rs.getInt("unlock"));
                shuShanBuildConfig.setParam(rs.getString("param"));

                if(shuShanBuildConfig.getType() == eShuShanBuildType.type_7.getType()){
                    List<Integer> paramList = StringUtils.stringToIntegerList(shuShanBuildConfig.getParam(),";");
                    for (int j = 0; j < paramList.size(); j++) {
                        shuShanBuildConfig.getWeightMap().put(j, paramList.get(j));
                    }
                }
                list.add(shuShanBuildConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getShuShanEventConfigSql = "select * from `t_s_activity_shushan_event` where `activityId`=?";
    public List<ShuShanEventConfig> getShuShanEventConfigList(int activityId) {
        List<ShuShanEventConfig> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getShuShanEventConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ShuShanEventConfig shuShanEventConfig = new ShuShanEventConfig();
                shuShanEventConfig.setActivityId(rs.getInt("activityId"));
                shuShanEventConfig.setId(rs.getInt("id"));
                shuShanEventConfig.setType(rs.getInt("type"));
                shuShanEventConfig.setIcon(rs.getString("icon"));
                shuShanEventConfig.setReward(rs.getString("reward"));
                shuShanEventConfig.setUnlock(rs.getInt("unlock"));

                // 解析奖励
                String[] rewards = shuShanEventConfig.getReward().split("\\|");
                for (int j = 0; j < rewards.length; j++) {
                    shuShanEventConfig.getOptionsRewardMap().put(j+1, rewards[j]);
                }
                list.add(shuShanEventConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getShuShanLiLianConfigSql = "select * from `t_s_activity_shushan_lilian` where `activityId`=?";
    public List<ShuShanLiLianConfig> getShuShanLiLianConfigList(int activityId) {
        List<ShuShanLiLianConfig> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getShuShanLiLianConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ShuShanLiLianConfig shuShanLiLianConfig = new ShuShanLiLianConfig();
                shuShanLiLianConfig.setActivityId(rs.getInt("activityId"));
                shuShanLiLianConfig.setId(rs.getInt("id"));
                shuShanLiLianConfig.setType(rs.getInt("type"));
                shuShanLiLianConfig.setReward(rs.getInt("reward"));
                shuShanLiLianConfig.setUnlock(rs.getInt("unlock"));
                shuShanLiLianConfig.setPic(rs.getInt("pic"));
                shuShanLiLianConfig.setpCode(rs.getInt("pCode"));
                shuShanLiLianConfig.setSpLimit(rs.getInt("spLimit"));
                shuShanLiLianConfig.setNameCode(rs.getInt("nameCode"));
                list.add(shuShanLiLianConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }
    private static String getShuShanSkillConfigSql = "select * from `t_s_activity_shushan_skill` where `activityId`=?";
    public List<ShuShanSkillConfig> getShuShanSkillConfigList(int activityId) {
        List<ShuShanSkillConfig> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getShuShanSkillConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ShuShanSkillConfig shuShanSkillConfig = new ShuShanSkillConfig();
                shuShanSkillConfig.setActivityId(rs.getInt("activityId"));
                shuShanSkillConfig.setId(rs.getInt("id"));
                shuShanSkillConfig.setType(rs.getInt("type"));
                shuShanSkillConfig.setLittleType(rs.getInt("littleType"));
                shuShanSkillConfig.setLevel(rs.getInt("level"));
                shuShanSkillConfig.setAdd(rs.getInt("add"));
                shuShanSkillConfig.setStudyP(rs.getInt("studyP"));
                list.add(shuShanSkillConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getShuShanRewardConfigSql = "select * from `t_s_activity_shushan_reward` where `activityId`=?";
    public List<ShuShanRewardConfig> getShuShanRewardConfigList(int activityId) {
        List<ShuShanRewardConfig> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getShuShanRewardConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ShuShanRewardConfig shuShanRewardConfig = new ShuShanRewardConfig();
                shuShanRewardConfig.setActivityId(rs.getInt("activityId"));
                shuShanRewardConfig.setId(rs.getInt("id"));
                shuShanRewardConfig.setLevel(rs.getInt("level"));
                shuShanRewardConfig.setPowerType(rs.getInt("powerType"));
                shuShanRewardConfig.setPower(rs.getInt("power"));
                shuShanRewardConfig.setCosttime(rs.getInt("costtime"));
                shuShanRewardConfig.setReward(rs.getString("reward"));
                list.add(shuShanRewardConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }
}
